//
//  NewsVC.m
//  APP_login
//
//  Created by edz on 7/6/21.
//  Copyright © 2021 baicai. All rights reserved.
//

#import "NewsVC.h"
#import "CustomTableViewCell.h"
#import "CustomTabCellModel.h"


@interface NewsVC ()
<
UITableViewDelegate,
UITableViewDataSource
>

@property (nonatomic,strong) UITableView *newsTabView;
@property (nonatomic,strong) NSArray *imageArray;
@property (nonatomic,strong) NSArray *nickNameArray;
@property (nonatomic,strong) NSArray *messageArray;
@property (nonatomic,strong) NSArray *timeArray;
@property (nonatomic,strong) NSMutableArray *modelArray;

@end

@implementation NewsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UILabel *titleLable = [[UILabel alloc] init];
    titleLable.text = @"消息";
    titleLable.font = [UIFont systemFontOfSize:25];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:titleLable];
    self.navigationItem.leftBarButtonItem = leftButton;
    UIBarButtonItem *createGroupsButton = [[UIBarButtonItem alloc] initWithTitle:@"创建群" style:UIBarButtonItemStylePlain target:self action:@selector(createGroups)];
    self.navigationItem.rightBarButtonItem = createGroupsButton;
    
    
    _newsTabView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_newsTabView];
    //自动调整子视图大小，避免tabbar遮住最后一个cell
    _newsTabView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _newsTabView.delegate = self;
    _newsTabView.dataSource = self;
    
    [_newsTabView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"news"];
    _imageArray = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11"];
    _messageArray = @[@"Makik:午好", @"TA起床了", @"互关秒回", @"结算测试", @"农场入场卷", @"问题意见", @"唐世平发布", @"学习结束", @"图片", @"我是。。。", @"蓝小红"];
    _nickNameArray = @[@"Timing小书童", @"一朵桃花", @"学习农场", @"小童", @"作文大赛", @"逢考必过", @"开心一夏夏", @"不想学习", @"小杨肖恩", @"汽车大赛",@"通知群"];
     _timeArray = @[@"20210719101845", @"20210719011545", @"20210718051045", @"20210718165405", @"20210715165405", @"20210714165405", @"20210713165405", @"20210712165405", @"20210702165405", @"20210701165405", @"20210602165405"];
     _modelArray = [[NSMutableArray alloc] init];
     for (int i = 0; i<= 10; i++) {
         CustomTabCellModel  *model = [[CustomTabCellModel alloc] init];
         model.image = _imageArray[i];
         model.nickName = _nickNameArray[i];
         model.message = _messageArray[i];
         model.time = [self makeTime:_timeArray[i]];
         [_modelArray addObject:model];
     }
     [_newsTabView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _modelArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"news" ];
    if (cell == nil) {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"news"];
    }
    cell.model = _modelArray[indexPath.row];
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (NSString*) makeTime:(NSString*) timeStr{
    //字符串转时间
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"yyyyMMddHHmmss";
    df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    NSDate *date = [[NSDate alloc] init];
    date = [df dateFromString:timeStr];
    
    //判断是否为今昨两天
    NSDateFormatter *dateFormartter = [[NSDateFormatter alloc] init];
    NSCalendar *calender = [NSCalendar currentCalendar];
    if ([calender isDateInToday:date]) {
        [dateFormartter setDateFormat:@"aa KK:mm"];
        return [dateFormartter stringFromDate:date];
    }
    if ([calender isDateInYesterday:date]) {
        return @"昨天";
    }
    //是否为一周内
    NSDate *nowDate = [df dateFromString:[df stringFromDate:[NSDate date]]];
    NSDateComponents *comp = [calender components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date toDate:nowDate options:0];
    if (comp.year == 0 & comp.month == 0 & comp.day <= 7) {
        [dateFormartter setDateFormat:@"EEE"];
        NSString *str =  [dateFormartter stringFromDate:date];
        if ([str isEqualToString:@"Mon"]) {
            return @"星期一";
        }
        if ([str isEqualToString:@"Tue"]) {
            return @"星期二";
        }
        if ([str isEqualToString:@"Wed"]) {
            return @"星期三";
        }
        if ([str isEqualToString:@"Thu"]) {
            return @"星期四";
        }
        if ([str isEqualToString:@"Fir"]) {
            return @"星期五";
        }
        if ([str isEqualToString:@"Sat"]) {
            return @"星期六";
        }
        if ([str isEqualToString:@"Sun"]) {
            return @"星期日";
        }
    }else{
        [dateFormartter setDateFormat:@"yyyy/MM/dd"];
        return [dateFormartter stringFromDate:date];
    }
   return nil;
}

-(void) createGroups{

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
